#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e5 + 10, INF = 0x3f3f3f3f;

int n, m;
vector<int> g[N];
int p[N], ans[N], rt[N], st[N];

struct DSU {
    std::vector<int> f;
    DSU() {}
    DSU(int n) { init(n); }
    void init(int n) {
        n++;
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
    }
    int find(int x) {
        if (x != f[x]) f[x] = find(f[x]);
        return f[x];
    }
    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return false;
        f[y] = x;
        return true;
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
}dsu;

void solve()
{
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> p[i];

    dsu.init(n);
    for (int i = 0;i <= n;i++) rt[i] = i, st[i] = 0;

    for (int i = 1;i <= n;i++) g[i].clear();
    for (int i = 1;i < n;i++) {
        int x, y;cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    for (int i = n;i > 0;i--) {
        int x = p[i];
        for (int y : g[x]) {
            int fy = dsu.find(y);
            if (st[fy] == 0) continue;
            ans[rt[fy]] = x;
            dsu.merge(x, fy);
        }
        x = dsu.find(x);
        rt[x] = x;st[x] = 1;
    }

    ans[p[1]] = 0;
    for (int i = 1;i <= n;i++) cout << ans[i] << ' ';
    cout << '\n';
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    cin >> T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}